package org.academic.texteditor.gui.tool;

import java.awt.Point;

import javax.swing.JLabel;
import javax.swing.SwingConstants;

import org.academic.texteditor.gui.component.ComponentFactory;
import org.academic.texteditor.gui.feature.Page;

public class PageNumberDecoration implements Decoration {

	private static final long serialVersionUID = 7838413889762969895L;

	public static final int INSETS = 20;
	public static final int CENTER = SwingConstants.CENTER; 
	public static final int LEFT = SwingConstants.WEST; 
	public static final int RIGHT = SwingConstants.EAST; 
	public static final int TOP = SwingConstants.NORTH; 
	public static final int BOTTOM = SwingConstants.SOUTH; 

	private final int horizontalPosition;
	private final int verticalPosition;

	private int pageNumber;
	
	private JLabel lblPageNumber;
	
	public PageNumberDecoration() {
		this(CENTER, BOTTOM);
	}
	
	public PageNumberDecoration(int horizontalPosition, int verticalPosition) {
		this.horizontalPosition = horizontalPosition;
		this.verticalPosition = verticalPosition;
	}

	public int getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}

	@Override
	public void decore(Page page) {
		int x = INSETS;
		int y = INSETS;
		
		int number = pageNumber++;
		page.setNumber(number);
		
		lblPageNumber = ComponentFactory.getLabel(String.valueOf(number));
		lblPageNumber.setHorizontalAlignment(JLabel.CENTER);
		lblPageNumber.setSize(50, 20);
		
		if (verticalPosition == TOP) {
			y = INSETS;
		} else if (verticalPosition == BOTTOM) {
			y = page.getHeight() - INSETS - lblPageNumber.getHeight();
		}
		
		if (horizontalPosition == LEFT) {
			x = INSETS;
		} else if (horizontalPosition == CENTER) {
			x = (page.getWidth() / 2) - (lblPageNumber.getWidth() / 2);
		} else if (horizontalPosition == RIGHT) {
			x = page.getWidth() - INSETS - lblPageNumber.getWidth();
		}
		
		Point location = new Point(x, y);
		lblPageNumber.setLocation(location);
		
		page.add(lblPageNumber);
	}

	@Override
	public void undecore(Page page) {
		page.remove(lblPageNumber);
	}
	
}
